package work.vcloud.baseinfo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;

import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Scope("prototype")
public class ChapterController extends BaseController {
	@Resource
	private OpenDao openDao;
	
	public OpenDao getOpenDao() {
		return openDao;
	}
	public void setOpenDap(OpenDao openDap) {
		this.openDao = openDao;
	}


	/**
	 * @Description 添加章节
	 */
	@RequestMapping("api/chapter.add")
	public OutputVo addChapter() {
		try {
			Map<String, Object> map = getParameterMap();

			String levelType = map.get("level_type").toString();
			String isParent = map.get("node_isleaf").toString();
			String nodeType = map.get("node_type").toString();
			if(levelType.equals("2")) {
				//子节点
				map.put("node_pid", map.get("node_id"));
				map.put("node_isleaf", "1");
				map.put("node_type", Integer.parseInt(nodeType) + 1);
			}

			map.put("sqlMapId","chapter.getMaxCode");
			Integer code = (Integer)openDao.queryForObject(map);
			if(code == 1) {
                map.put("code", map.get("code").toString() + "01");
			}else{
                map.put("code", code);
            }
			map.put("sqlMapId", "chapter.insert");
			openDao.insert(map);

			//更新章节为父章节
			if(levelType.equals("2") && isParent.equals("1")) {
				map.put("sqlMapId", "chapter.packedInParentNode");
				map.put("node_isleaf", "0");
				openDao.update(map);
			}
			return output("");
		} catch (Exception e) {
			e.printStackTrace();
			return output(e);
		}
	}

	/**
	 * @Description 删除节点
	 */
	@RequestMapping("api/chapter.remove")
	public OutputVo removeChapter() {
		try {
			Map<String, Object> map = getParameterMap();
			map.remove("page");

			String nodeId = map.get("node_id").toString();

			//1.更新序号以及是否为父节点
            updateNodeInfo(nodeId);

			//2.所有要删除的节点
			map.put("sqlMapId", "chapter.selectThisAndChildrenNode");
			List<Map<String, Object>> dataList= openDao.queryForList(map);

			if(dataList.size() > 0) {
				for (int i = 0; i < dataList.size(); i++) {
					//章节id
					map.put("node_id", dataList.get(i).get("node_id"));
					map.put("sqlMapId", "chapter.delete");
					openDao.update(map);
				}
			}

			return output("");
		} catch (Exception e) {
			e.printStackTrace();
			return output(e);
		}
	}

	/**
	 * @Description 更新节点序号以及是否为父节点
	 * @param nodeId
	 */
	public void updateNodeInfo(String nodeId) {
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			//1. 查询当前删除的节点
			map.put("node_id", nodeId);
			map.put("sqlMapId", "chapter.selectById");
			Map<String, Object> currentDeleteNode = (Map)openDao.queryForObject(map);

			//2.查询当前节点和所有同级节点
			map.put("fk_subject", currentDeleteNode.get("fk_subject"));
			map.put("node_pid", currentDeleteNode.get("node_pid"));
			map.put("node_type", currentDeleteNode.get("node_type"));
			int currentNodeOrderNumber = Integer.parseInt(currentDeleteNode.get("order_number").toString());
			map.put("sqlMapId", "chapter.selectThisAndBrotherNode");

			List<Map<String, Object>> dataList = openDao.queryForList(map);
			//3.如果同级节点为null，更新删除节点的父节点的node_isleaf为1
			if(dataList.size() <= 1) {
				map.put("node_isleaf", "1");
				map.put("node_id", currentDeleteNode.get("node_pid"));
				map.put("sqlMapId", "chapter.packedInParentNode");
				openDao.update(map);
			}else{
				//否则，更新节点序号
				for (int i = 0; i < dataList.size(); i++) {
					int orderNumber = Integer.parseInt(dataList.get(i).get("order_number").toString());
					if(orderNumber > currentNodeOrderNumber) {
						map.put("node_id", dataList.get(i).get("node_id"));
						map.put("order_number", orderNumber - 1);
						map.put("sqlMapId", "chapter.updateOrderNumber");
						openDao.update(map);
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Description 更新节点序号
	 */
	@RequestMapping("api/chapter.updateOrderNumber")
	public OutputVo updateOrderNumber() {
		Map<String, Object> map = getParameterMap();

		try {
			JSONArray nodes  = (JSONArray)JSON.parse(map.get("nodes").toString());
			map.put("sqlMapId", "chapter.updateOrderNumber");
			for (int i = 0; i < nodes.size(); i++) {
				Map<String, Object> node = (Map)nodes.get(i);
				map.put("order_number", node.get("orderNumber"));
				map.put("node_id", node.get("id"));

				openDao.update(map);
			}

			return output("");
		} catch (Exception e) {
			e.printStackTrace();
			return output(e);
		}
	}
	
}
